/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.api;

import cn.hallele.infrastructure.metadata.io.R;
import cn.hallele.infrastructure.metadata.io.Response;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.api.TenantApi;
import cn.hallele.mc.sdk.portalcenter.common.constants.ReqPathConst;
import cn.hallele.mc.sdk.portalcenter.dto.OptionDTO;
import cn.hallele.mc.sdk.portalcenter.dto.TenantDTO;
import cn.hallele.mc.sdk.portalcenter.dto.TenantQueryDTO;
import cn.hallele.mc.sdk.usercenter.common.enums.TenantStatusEnum;
import cn.hallele.ms.infrastructure.auditlog.annotation.ApiLog;
import cn.hallele.ms.portalcenter.das.TenantDBDas;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Range;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/13 11:36
 */
@RestController
@RequestMapping(ReqPathConst.TENANT_V1)
public class TenantController implements TenantApi {
    @Autowired
    private TenantDBDas tenantDBDas;

    /**
     * 获取租户列表
     *
     * @param condition condition
     * @return List
     */
    @Override
    @ApiLog(id = "TENANT_V1_TENANT")
    public Response<List<TenantDTO>> findAll(@Valid @ParameterObject @SpringQueryMap TenantQueryDTO condition) {
        List<TenantDTO> result = tenantDBDas.findAllMoreCondition(condition);
        return R.ok(result);
    }

    /**
     * 添加租户
     *
     * @param tenantDTO tenantDTO
     * @return List
     */
    @Override
    @ApiLog(id = "TENANT_V1_ADD_TENANT")
    public Response<Void> addTenant(@Valid TenantDTO tenantDTO) {
        tenantDBDas.addTenant(tenantDTO);
        return R.ok(null);
    }

    /**
     * 修改租户
     *
     * @param tenantDTO tenantDTO
     * @return List
     */
    @Override
    @ApiLog(id = "TENANT_V1_UPDATE_TENANT")
    public Response<Void> updateTenant(@Valid TenantDTO tenantDTO) {
        tenantDBDas.updateTenant(tenantDTO);
        return R.ok(null);
    }

    /**
     * 获取租户下拉选项
     *
     * @return List
     */
    @Override
    @ApiLog(id = "TENANT_V1_GET_OPTIONS")
    public Response<List<OptionDTO<TenantDTO, Integer>>> getOptions() {
        List<TenantDTO> result = tenantDBDas.findAll(TenantDTO.builder().status(TenantStatusEnum.NORMAL.getCode()).build());
        List<TenantDTO> tree = ObjectUtil.toTree(result, 0);
        List<OptionDTO<TenantDTO, Integer>> optionDTOS =
                OptionDTO.buildList(tree, TenantDTO::getName,
                        TenantDTO::getId,
                        TenantDTO::getParentId,
                        t -> {
                            if (ObjectUtil.isNotNull(t.getChildren())) {
                                return OptionDTO.buildList(t.getChildren(), TenantDTO::getName, TenantDTO::getId);
                            }
                            return null;
                        });
        return R.ok(optionDTOS);
    }

    /**
     * 获取租户详情
     *
     * @param id id
     * @return TenantDTO
     */
    @Override
    @ApiLog(id = "TENANT_V1_GET_TENANT")
    public Response<TenantDTO> getTenant(@NotNull @Range(max = 2147483647L) Integer id) {
        return R.ok(tenantDBDas.getTenant(id));
    }

    /**
     * 删除租户
     *
     * @param ids ids
     * @return TenantDTO
     */
    @Override
    @ApiLog(id = "TENANT_V1_DELETE_TENANT")
    public Response<Void> deleteTenant(@NotEmpty List<Integer> ids) {
        tenantDBDas.deleteTenant(ids);
        return R.ok(null);
    }
}
